﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using SP_XHWeb.WebParts.EmployeeInfo;
using Microsoft.SharePoint;
using System.Linq;
using System.Text;

namespace SP_XHWeb.ControlTemplates.SP_XHWeb
{
    public partial class NewEmployee : UserControl
    {
        public string EmployeeInfoUrl = "/_layouts/SP_XHWeb/EmployeeDetails.aspx?item_id=";
        public string birthName = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblEmployee.InnerHtml = InitEmployee();
                if (!string.IsNullOrEmpty(birthName))
                {
                    divBirth.Style["display"] = "block";
                    lblBirth.InnerText = "祝贺 " + birthName.Substring(0,birthName.LastIndexOf(',')) + " 生日快乐！";
                }
                else
                {
                    divBirth.Style["display"] = "none";
                }
            }
        }


        private string InitEmployee()
        {
            StringBuilder str = new StringBuilder();
            List<SPListItem> empLists = GetNewEmplyees();
            
            str.Append("<ul>");

            if (empLists != null && empLists.Count !=0)
            {
                for (int i = 0; i < empLists.Count; i++)
                {
                    string linkUrl = EmployeeInfoUrl + empLists[i].ID;
                    str.Append("<li>");
                    str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "欢迎 " + empLists[i].Title + " 于 " + DateTime.Parse(empLists[i]["入职日期"].ToString()).ToString("yyyy年MM月dd日") + " 加入星浩");
                    
                    str.Append("</li>");
                }
            }

            str.Append("</ul>");
            return str.ToString();
        }


        protected List<SPListItem> GetNewEmplyees()
        {
            List<SPListItem> emplists = new List<SPListItem>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            if (!string.IsNullOrEmpty(hostUrl))
            {
                using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList("EmployeeInfo");
                        if (list != null && list.ItemCount != 0)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                emplists.Add(item);
                                if (DateTime.Parse(item["生日"].ToString()).ToString("MM-dd") == DateTime.Today.ToString("MM-dd"))
                                {
                                    birthName += item.Title + ",";
                                }
                            }
                            //Linq查询最新入职前5条
                            emplists = (from emp in emplists orderby emp["入职日期"] descending select emp).Take(5).ToList<SPListItem>();
                        }
                    }
                }
            }
            return emplists;
        }
    }
}
